#include "custom_warning.h"
#include "ui_custom_warning.h"

Custom_warning::Custom_warning(QString _text, Custom_warning::PUSHBUTTON_NAME _type, QWidget *parent) :
    QDialog(parent),
    label_text(_text), type(_type), ui(new Ui::Custom_warning)
{
    ui->setupUi(this);

    Ui_Init();


}

Custom_warning::~Custom_warning()
{
    delete ui;
}

/**
* @brief UI init
* @retval None
*/
void Custom_warning::Ui_Init(void)
{
    this->setWindowFlags(Qt::FramelessWindowHint);      //设置为无边框窗口
    this->setAttribute(Qt::WA_TranslucentBackground);   //设置为透明

    //禁用右键菜单
    ui->label_icon->setContextMenuPolicy(Qt::NoContextMenu);
    ui->label_text->setContextMenuPolicy(Qt::NoContextMenu);
    setWindowModality(Qt::ApplicationModal); //阻塞除当前窗体之外的所有的窗体

    QPixmap originalPixmap(":/pic/success.png"); // 加载原始图片
    QPixmap scaledPixmap = originalPixmap.scaled(40, 40, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 平滑缩放图片保持长宽比
    ui->label_icon->setPixmap(scaledPixmap); // 设置缩放后的图片给 QLabel

    ui->pushButton_close->setIcon(QIcon(":/pic/close.png"));

    ui->label_text->setText(label_text);

    switch (type)
    {
        case PUSHBUTTON_NAME::OK:
            ui->pushButton->setText("OK");
            break;
        case PUSHBUTTON_NAME::NO:
            ui->pushButton->setText("NO");
            break;
        case PUSHBUTTON_NAME::YES:
            ui->pushButton->setText("YES");
            break;
    }
    //提示声
    QApplication::beep();
}


/**
* @brief 获取输入框的值
* @param text:输入文本
* @param type:按键值
* @retval
*/
int Custom_warning::Display(QString text, Custom_warning::PUSHBUTTON_NAME type,QWidget *parent)
{
    Custom_warning *custom_warning = new Custom_warning(text, type ,parent);

    custom_warning->exec(); // 弹出模态对话框并等待用户操作

    return custom_warning->ret_type;
}


/**
* @brief 关闭 槽函数
* @retval None
*/
void Custom_warning::on_pushButton_close_clicked()
{
    this->close();
}

/**
* @brief 按钮 槽函数
* @retval None
*/
void Custom_warning::on_pushButton_clicked()
{
    this->close();
    ret_type = PUSHBUTTON_NAME::YES;
}


/*===========================================无边框初始化==================================================*/
//鼠标按下事件
/*
 *作用：
 *1.判断是否时左键点击 _isleftpressed
 *2.获取光标在屏幕中的位置 _plast
 *3.左键按下时光标所在区域 _curpos
 */
void Custom_warning::mousePressEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    if (event->button() == Qt::LeftButton)
    {
        this->_isleftpressed = true;
        QPoint temp = event->globalPos();
        _plast = temp;
        _curpos = countFlag(event->pos(), countRow(event->pos()));
    }
}


//鼠标释放事件
/*
 *作用：
 *1.将_isleftpressed 设为false
 *2.将光标样式恢复原样式  setCursor(Qt::ArrowCursor);
 */
void Custom_warning::mouseReleaseEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    if (_isleftpressed)
        _isleftpressed = false;
    setCursor(Qt::ArrowCursor);
}


//鼠标移动事件
void Custom_warning::mouseMoveEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    if(this->isFullScreen()) return;    //窗口铺满全屏，直接返回，不做任何操作
    int poss = countFlag(event->pos(), countRow(event->pos()));
    setCursorType(poss);
    if (_isleftpressed)//是否左击
    {
        QPoint ptemp = event->globalPos();
        ptemp = ptemp - _plast;
        if (_curpos == 22)//移动窗口
        {
            ptemp = ptemp + pos();
            move(ptemp);
        }
        else
        {
            QRect wid = geometry();
            switch (_curpos)//改变窗口的大小
            {
            case 11:wid.setTopLeft(wid.topLeft() + ptemp); break;//左上角
            case 13:wid.setTopRight(wid.topRight() + ptemp); break;//右上角
            case 31:wid.setBottomLeft(wid.bottomLeft() + ptemp); break;//左下角
            case 33:wid.setBottomRight(wid.bottomRight() + ptemp); break;//右下角
            case 12:wid.setTop(wid.top() + ptemp.y()); break;//中上角
            case 21:wid.setLeft(wid.left() + ptemp.x()); break;//中左角
            case 23:wid.setRight(wid.right() + ptemp.x()); break;//中右角
            case 32:wid.setBottom(wid.bottom() + ptemp.y()); break;//中下角
            }
            setGeometry(wid);
        }
        _plast = event->globalPos();//更新位置
    }
}


//获取光标在窗口所在区域的 列  返回行列坐标
int Custom_warning::countFlag(QPoint p,int row)//计算鼠标在哪一列和哪一行
{
    if(p.y()<MARGIN)
        return 10+row;
    else if(p.y()>this->height()-MARGIN)
        return 30+row;
    else
        return 20+row;
}


//获取光标在窗口所在区域的 行   返回行数
int Custom_warning::countRow(QPoint p)
{
    return (p.x()<MARGIN) ? 1 : (p.x()>(this->width() - MARGIN) ? 3 : 2);
}


//根据鼠标所在位置改变鼠标指针形状
void Custom_warning::setCursorType(int flag)
{
    switch(flag)
    {
    case 11:
    case 33:
        setCursor(Qt::SizeFDiagCursor);
        break;
    case 13:
    case 31:
        setCursor(Qt::SizeBDiagCursor);break;
    case 21:
    case 23:
        setCursor(Qt::SizeHorCursor);break;
    case 12:
    case 32:
        setCursor(Qt::SizeVerCursor);break;
    case 22:
        setCursor(Qt::ArrowCursor);
        QApplication::restoreOverrideCursor();//恢复鼠标指针性状
        break;
    }
}


/**
* @brief 重写绘图事件
* @retval None
*/
void Custom_warning::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true); // 设置抗锯齿
//    painter.setPen(QPen(QColor(180, 179, 181), 5)); // 设置边框颜色和宽度为

    // 创建线性渐变色
    QLinearGradient gradient(0, 0, 0, this->height());
    gradient.setColorAt(0, QColor(245, 245, 247)); // 起始颜色
    gradient.setColorAt(1, QColor(240, 239, 238)); // 终止颜色

    // 使用渐变色填充背景
    painter.setBrush(gradient);
    painter.setPen(Qt::NoPen);
    painter.drawRoundedRect(0, 0, this->width(), this->height(), 10, 10); // 绘制圆角矩形
}

